package com.cyy.log.spark_extract.common

import java.util.{Date, Locale}

import org.apache.commons.lang3.time.FastDateFormat

/**
  * @Author: Cyy
  * @Description:
  * @Date:Created in 21:56 2019/5/7
  */
object DateUtils {
  val YYYYMMDDHHMM_TIME_FORMAT = FastDateFormat.getInstance("dd/MMM/yyyy:HH:mm:ss Z", Locale.ENGLISH)
  val TARGET_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss")

  def parse(time: String) = {
    TARGET_FORMAT.format(new Date(getTime(time)))
  }
  def getTime(time: String) = {
    try {
      YYYYMMDDHHMM_TIME_FORMAT.parse(time.substring(time.indexOf("[") + 1,
        time.lastIndexOf("]"))).getTime
    } catch {
      case e: Exception => {
        0l
      }
    }
  }
  def main(args: Array[String]) {
    println(parse("[05/May/2019:01:57:35 -0400]"))
  }
}
